package com.zhanglinwei.openApiClient.utils;

import com.baomidou.mybatisplus.core.toolkit.support.*;
import com.zhanglinwei.openApiClient.exception.OpenApiParseException;

import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.function.Function;

public class LambdaUtils {

    private LambdaUtils(){}

    public static <T> SerializedLambda parseLambda(Function<T, ?> column) {
        try {
            Method method = column.getClass().getDeclaredMethod("writeReplace");
            method.setAccessible(true);
            return (SerializedLambda)method.invoke(column);
        } catch (Exception ex) {
            throw new OpenApiParseException("Failed to retrieve serialize lambda", ex);
        }
    }
}
